package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.Multimedia;

/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class DAO_Multimedia {
    private int i;
   
    private static volatile DAO_Multimedia instaceDAO_Multimedia = new DAO_Multimedia();
    
    /** The private constructor of DAO_Multimedia for singleton
    */
    private DAO_Multimedia(){
    
    }
    
    /** The method for get the instance of the class DAO_Multimedia
    * @return DAO_Multimedia instaceDAO_Multimedia
    */
    public static DAO_Multimedia getInstance() {
        //if(instaceDAO_Multimedia==null)
           synchronized(DAO_Multimedia.class) {
             if (instaceDAO_Multimedia == null)
               instaceDAO_Multimedia = new DAO_Multimedia();
           }
        return instaceDAO_Multimedia;
    }
    
    public int insertMultimedia(Multimedia multimedia){
//        String createSeqCollection = "create sequence seqmax_multimedia increment by 1;";
        String insertMultimedia = "insert into Multimedia values(nextval('seqmax_multimedia'),'"+multimedia.getTYPE()+"');";

        String getIdMultimedia = "select max(ID_MULTIMEDIA) from multimedia";
        
         PgConnect conn = PgConnect.getInstance();
        try {
           
            conn.executeInsert(insertMultimedia);
            ResultSet rs = conn.executeQuery(getIdMultimedia);
            while (rs.next()) {
                i = rs.getInt("max");
            }
            conn.Close();
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Multimedia.class.getName()).log(Level.SEVERE, null, ex);
            conn.Close();
        }
        
        return i;
    }
    
//    public static void main(String[] args){
//        DAO_Multimedia dm = DAO_Multimedia.getInstance();
//        Multimedia mm = new Multimedia();
//        mm.setTYPE("photo");
//        int i = dm.insertMultimedia(mm);
//        System.out.println(i);
//    }
}
